package com.example.bookexchange;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class CitySelectedListener implements AdapterView.OnItemSelectedListener {

    private final Context context;
    private final Spinner spinnerDistrict;

    public CitySelectedListener(Context context, Spinner spinnerDistrict) {
        this.context = context;
        this.spinnerDistrict = spinnerDistrict;
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        int districtArrayId;
        String selectedCity = parent.getItemAtPosition(position).toString();

        if (selectedCity.equals("广州市")) {
            districtArrayId = R.array.guangzhou_districts;
        } else if (selectedCity.equals("杭州市")) {
            districtArrayId = R.array.hangzhou_districts;
        } else {
            districtArrayId = R.array.default_districts;
        }

        ArrayAdapter<CharSequence> districtAdapter = ArrayAdapter.createFromResource(
                context, districtArrayId, android.R.layout.simple_spinner_item);
        districtAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerDistrict.setAdapter(districtAdapter);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {}
}
